package MyString;

public class BruteForce {
    // 实现 Brute-Force 算法类
    public static int BF(SqString originalStr, SqString comparedStr) throws Exception {
        if(comparedStr.length()<=originalStr.length() && comparedStr!=null && originalStr!=null){
            // 外层循环原始字符串  如果不匹配原始表往前走
            for(int i=0; i<originalStr.length(); i++){
                int tmpLen = 0;
                for(; tmpLen<comparedStr.length(); tmpLen++){
                    // 内层循环带匹配字符串
                    if(originalStr.charAt(i+tmpLen) == comparedStr.charAt(i)){
                        continue;
                    }else {
                        break;
                    }
                }
                // 如果 最后tmpLen 和 comparedStr 长度一致就是全部走完都能匹配上
                if(tmpLen == comparedStr.length()){
                    return i;
                }else if(comparedStr.length() > (originalStr.length()-i)){
                    break;
                }
            }

        }
        return -1;
    }


    public static void main(String[] args) throws Exception {
        SqString q = new SqString("aaaaaa");
        SqString p = new SqString("aaaaaa");

        int value = BruteForce.BF(q, p);
        System.out.println(value);
    }

}
